package com.kbbywyclient.presenter;

import android.content.Context;
import com.kbbywyclient.R;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.BankCardInfoContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.customservice.ApiException;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/14 16:52.
 */

public class BankCardInfoPresenter extends BasePresenter implements BankCardInfoContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    private BankCardInfoContract.View view;
    @Inject
    public BankCardInfoPresenter(BankCardInfoContract.View view){
        this.view=view;
    }
    @Override
    public void checkBankInfo(int userId,String name, String bankCardNumber, String idCardNo, String phone) {
        view.showLoadingDialog(mContext.getString(R.string.loading));
        jsonUtil.put("salesID",userId);
        jsonUtil.put("trueName",name);
        jsonUtil.put("bankAccount",bankCardNumber);
        jsonUtil.put("IDCardNo",idCardNo);
        jsonUtil.put("bankMobile",phone);

        ApiServiceHelper.appointThreadNormal(apiService.checkBankCardInfo(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<BaseModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        if(e instanceof ApiException){
                            ApiException ex= (ApiException) e;
                            if(ex.getmErrorCode()==2){
                                HttpExcUtil.printError(e);
                            }else{
                                view.checkBankInfoError();
                            }
                        }else{
                            HttpExcUtil.printError(e);
                        }
                    }

                    @Override
                    public void onNext(BaseModel model) {
                        view.checkBankInfoSuccess(model);
                    }
                });
    }
}
